import router from '@/router'
import axios from 'axios'
import qs from 'qs'

// axios.defaults.baseURL = 'https://cnodejs.org/api/v1/'';
var port = process.env.NODE_ENV === 'production' ? 80 : 8080;
axios.defaults.baseURL = 'http://www.vuecnode.com:'+port+'/api/';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.withCredentials = true; //firefox

axios.defaults.transformRequest = [function(data) {
	//console.log(qs.stringify(data)); 
	//return a string or an instance of Buffer, ArrayBuffer,FormData or Stream
	return qs.stringify(data);
}];

axios.defaults.timeout = 60000;


axios.defaults.validateStatus = function(status) {
	return status >= 200 && status < 300 || status == 403 || status == 401;
}

// Add a response interceptor
axios.interceptors.response.use(function(response) {
	if (response.data.error_msg) {
		if (response.data.error_msg == '错误的accessToken') {
			if(location.href.indexOf('login') < -1){
				location.href = '/login';
				window.location.reload()
			}
			// return Promise.reject(response.data);
		}
	}
	return response;
}, function(error) {
	return Promise.reject(error);
});

function requestEntrance(data, callback) {
	if (typeof(callback) === 'object') {
		data = data ? data : {};
		callback.success = callback.success || function() {};
		callback.error = callback.error || function() {};
		callback.complete = callback.complete || function() {};

		axios(data)
			.then(function(response) {
				// console.log(response)
				if (response) {
					callback.complete();
					callback.success(response.data);
				}
			})
			.catch(function(error) {
				callback.complete();
				callback.error(error);
				if (error && error.error_msg) {
					console.log(error.error_msg)
				} else {
					alert('Oops! There was a problem connecting to the network. Please refresh!(' + data.url + ')');
				}
				console.log('console 110 error:', data.url, error);
			});
	}
}


export function login(obj) {
	let requestObj = {
			method: 'POST',
			url: 'login',
			// data: obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};
	console.log(requestObj)
	requestEntrance(requestObj, callbackObj);
}

export function accesstoken(obj) {
	let requestObj = {
			method: 'POST',
			url: 'accesstoken',
			data: obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};

	requestEntrance(requestObj, callbackObj);
}


export function user(obj) {
	let requestObj = {
			method: 'GET',
			url: 'user/' + obj.userName,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};

	requestEntrance(requestObj, callbackObj);
}


export function topic(obj) {
	let requestObj = {
			method: 'GET',
			url: 'topic/' + obj.id,
			params:obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};

		console.log(requestObj)

	requestEntrance(requestObj, callbackObj);
}

export function topic_collect(obj) {
	let requestObj = {
			method: 'POST',
			url: 'topic_collect/' + obj.type,
			data:obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};

		console.log(requestObj)

	requestEntrance(requestObj, callbackObj);
}

export function reply(obj) {
	// var vData = Object.assign({}, obj.data);
	// delete vData.topicId;
	let requestObj = {
			method: 'POST',
			url: 'topic/' + obj.data.topicId + '/replies',
			data: obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};

	requestEntrance(requestObj, callbackObj);
}


export function thumb(obj) {
	let requestObj = {
			method: 'POST',
			url: 'reply/' + obj.data.id + '/ups',
			data: obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};

	requestEntrance(requestObj, callbackObj);
}

export function topics(obj) {
	var _url = 'page='+obj.data.page+'&limit='+obj.data.limit+'&tab='+obj.data.tab+'&mdrender=true';

	let requestObj = {
			method: 'GET',
			url:'topics?'  + _url,
			data: obj.data,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};
	requestEntrance(requestObj, callbackObj);
}

export function collect(obj) {
	let requestObj = {
			method: 'GET',
			url:'topic_collect/'  + obj.data.name,
		},
		callbackObj = {
			success: obj.success,
			complete: obj.complete,
			error: obj.error,
		};
	requestEntrance(requestObj, callbackObj);
}
